package com.wcgksoft.controller.alarm;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.Alert.Alarm;
import com.wcgksoft.entity.Alert.AlarmHistory;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.alarm.AlarmHistoryService;
import com.wcgksoft.service.alarm.AlarmService;
import com.wcgksoft.service.mainten.MaintenService;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.alarm
 * @Description TODO
 * @date 2020/6/4 15:28
 * @ClassName AlarmHistoryController
 */
@Controller
@RequestMapping("alarmHistory")
public class AlarmHistoryController {

    @Autowired
    private AlarmHistoryService alarmHistoryService;

    /**
     * 查询告警历史记录
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(AlarmHistory.class);
        criteria.add(Restrictions.eq("enterpriseid",sysUser.getEnterprise().getId()));
        List<AlarmHistory> list = alarmHistoryService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", alarmHistoryService.getTotalCount(), list);
    }


    /**
     * 五种属性柱状图告警统计
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findDataFive")
    public MsgInfo findDataFive(){
        MsgInfo msgInfo = new MsgInfo();
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        List<Object[]> wd = this.find(sysUser.getEnterprise().getId(), "wd");
        List<Object[]> sd = this.find(sysUser.getEnterprise().getId(), "sd");
        List<Object[]> aq = this.find(sysUser.getEnterprise().getId(), "aq");
        List<Object[]> co2 = this.find(sysUser.getEnterprise().getId(), "co2");
        List<Object[]> gzqd = this.find(sysUser.getEnterprise().getId(), "gzqd");


        String[] temp = new String[12];
        String[] water = new String[12];
        String[] nh3 = new String[12];
        String[] co22 = new String[12];
        String[] wind = new String[12];

        for (int i = 0; i < 12; i++) {
            temp[i] = wd.get(i)[1].toString();
            water[i] = sd.get(i)[1].toString();
            nh3[i] = aq.get(i)[1].toString();
            co22[i] = co2.get(i)[1].toString();
            wind[i] = gzqd.get(i)[1].toString();
        }

        Map<String, String[]> map = new HashMap<>();
        map.put("wendu",temp);
        map.put("shidu",water);
        map.put("NH3",nh3);
        map.put("co2",co22);
        map.put("gzqd",wind);
        msgInfo.setData(map);

        return msgInfo;
    }

    public List<Object[]> find(Integer id,String type){
        List<Object[]> windSpeed = alarmHistoryService.getHibernateSession().createSQLQuery("SELECT CASE datas.weeks  " +
                "  WHEN '1' THEN '1'  WHEN '2' THEN '2'  WHEN '3' THEN '3' WHEN '4' THEN '4'  " +
                "  WHEN '5' THEN '5'  WHEN '6' THEN '6'  WHEN '7' THEN '7' WHEN '8' THEN '8'  " +
                "  WHEN '9' THEN '9'  WHEN '10' THEN '10'  WHEN '11' THEN '11'  " +
                "  WHEN '12' THEN '12'  END AS 'week', IFNULL( datas.totalUser, 0 ) totalUser  FROM (SELECT a.d weeks, b.totalUser  FROM ( " +
                "  SELECT " +
                "   1 AS d UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT " +
                "   6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT " +
                "   11 UNION ALL SELECT " +
                "   12 ) a LEFT JOIN ( SELECT date_format( us.onealarmtime, '%m' ) AS datetime, COUNT( us.id ) AS totalUser  FROM t_alarmHistory us  WHERE " +
                " enterpriseid=' " + id + "' and typecode= '"+type+"'" +
                "  and YEAR ( us.onealarmtime )= YEAR ( NOW())  GROUP BY datetime) b ON a.d = b.datetime) datas ORDER BY datas.weeks ASC").list();
        return windSpeed;
    }

}
